rm(list = ls(all.names = TRUE))

library(foreign)
library(dplyr)
library(readstata13)
library(ggplot2)
library(arm)
ve



Data <- read.dta13("~/Dropbox/Mingling for IO/ICC paper data/IO Replication PUTNAM/Layering_Data_Oct_2019 replication.dta")
Data <- subset(Data, year == 2002 )

#note Montenegro has missing value for july02_exposure

sort(names(Data))

######################################
###Number of countries by Assurance Score
#Area Plot 2002

#to get straight drop down of lines


Data$joindummy <- 0
Data[Data$earlyrat==1 & Data$iccmember==1,]$joindummy <- 1 #Early joiners
Data[Data$earlyrat==0 ,]$joindummy <- 2 #Late ICC joiners
#Data[Data$iccmember==0,]$joindummy <-3 #Non-joiners

with(Data, tapply(july02_exposure,joindummy, range, na.rm=TRUE))



country_count <- data.frame(Data %>% group_by(july02_exposure, joindummy) %>% summarize(n()))
colnames(country_count) <- c("july02_exposure", "joindummy","score_count")

#I added in rows where there were 0s, which eliminates gaps in the area graph
#country_count <- add_row(country_count,exposure4 = -3,joindummy = 2,score_count = 0)
#country_count <- add_row(country_count,exposure4 = -3,joindummy = 3,score_count = 0)
#country_count <- add_row(country_count,exposure4 = -2,joindummy = 3,score_count = 0)
#country_count <- add_row(country_count,exposure4 = -1,joindummy = 3,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 6,joindummy = 1,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 6,joindummy = 2,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 7,joindummy = 1,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 7,joindummy = 2,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 8,joindummy = 1,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 8,joindummy = 2,score_count = 0)

country_count$joinwords <- NA
country_count[country_count$joindummy==1,]$joinwords <- "Early Joiners"
country_count[country_count$joindummy==2,]$joinwords <- "Other States"
#country_count[country_count$joindummy==3,]$joinwords <- "Non-Joiners"

g4 <- ggplot(data=country_count,
             aes(x=july02_exposure,
                 y=score_count,
                 fill=joinwords
                 #color=joinwords
             ))
#g4 <- g4 + geom_line(size=1, aes(linetype=joinwords)) 

g4 <- g4 + geom_point(size=1,alpha=.8 #,position=position_jitter(h=0.1,w=0.1)
) 
g4 <- g4 + geom_area(position="identity",alpha=.5, size=.2, color="black")
g4 <- g4 + scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3,4,5,6,7), label=c(-3,-2,-1,0,1,2,3,4,5,6,7), limits = c(-3,7))
g4 <- g4 + theme_minimal() 
g4 <- g4 + scale_fill_manual(values = c("darkgrey", "white"))
#g4 <- g4 + scale_color_manual(values = c("#C0C0C0", "black","#707070"))
#g4 <- g4 + scale_linetype_manual(values=c("solid", "dotted", "dashed"))
g4 <- g4 + guides(size=FALSE, color=FALSE, linetype=FALSE, point=FALSE)
g4 <- g4 + labs(x="Assurance Score",
                y="Number of Countries",
                title="Early ICC Ratifers and Other States in 2002",
                #shape="ICC Join Date"
                fill="ICC Join Date"
)
g4 <- g4 + theme(legend.position="bottom",
                 axis.text.x = element_text(size=12, face="bold"),
                 axis.text.y = element_text(size=12, face="bold"),
                 axis.line.y = element_line(colour = "grey", size = .5,linetype = "solid"),
                 axis.line.x = element_line(colour = "grey", size = .5,linetype = "solid"),
                 plot.title = element_text(family = "sans", size = 14, margin=margin(0,0,10,0),face="bold", hjust=0.5),
                 panel.grid.minor.x=element_blank(),
                 panel.grid.minor.y=element_blank(),
                 axis.title.x = element_text(size=14,face="bold"),
                 axis.title.y = element_text(size=14,face="bold"))
pdf("descriptive_2002.pdf")
plot(g4)
dev.off()

#note Montenegro has missing value for july02_exposure
